package com.example.yang.ministy;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.ArraySet;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by Yang on 2016/9/12.
 */
public class Activity_test extends AppCompatActivity{
    SharedPreferences sharedPreferences;
    private TextView write;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_tv);
        editor=sharedPreferences.edit();
        sharedPreferences=getSharedPreferences("frame_tv", Context.MODE_PRIVATE);
        write=(TextView)findViewById(R.id.write);
    }
    public void write(View view){
        editor.putBoolean("isFirstRun",true );
        editor.putFloat("hight",1.78f);
        editor.putInt("age",25);
        editor.putLong("birth", System.currentTimeMillis());
        editor.putString("name","张三丰");
        List<String> tagList=new ArrayList<>(Arrays.asList(new String("小龙女"+"/n"+"杨过"+"/n"+"洪七公"+"/n"+"黄药师")));
        Set<String> tag=new HashSet<>();
        tag.addAll(tagList);
        Set<String> tags=new HashSet<>();
        tags.add("英俊");
        tags.add("潇洒");
        tags.add("风流");
        tags.add("倜傥");
        editor.putStringSet("dms",tags);
        editor.putStringSet("dms1",tag);
        editor.commit();
    }
    public void read(View view){
        final boolean isFirstRun=sharedPreferences.getBoolean("isFirstRun",false);
        final float hight=sharedPreferences.getFloat("hight",0);
        final int age=sharedPreferences.getInt("age",0);
        final long birth=sharedPreferences.getLong("birth",0);
        final String name=sharedPreferences.getString("name","");
        final Set<String> tag=sharedPreferences.getStringSet("dms1",null);
        final Set<String> tags=sharedPreferences.getStringSet("dms",null);
        SimpleDateFormat Format=new SimpleDateFormat("yyyy_MM_dd HH:mm:ss ");
        write.setText("是否第一次运行;"+(isFirstRun ? "是":"否")+"\n"+"身高;"+hight+"米\n"+"年龄:"+age+"岁\n"+"出生日期;"+Format.format(new Date(birth))+"\n"+"标签:"+getTags(tags)+"\n"+"标签1:"+getTags(tags));


    }
    private String getTags(Set<String> tags){
        if (tags==null||tags.size()==0)return "";
        Iterator iterator=tags.iterator();
        StringBuffer v=new StringBuffer();
        while (iterator.hasNext()){
            v.append(iterator.next()+",");
        }
        return v.toString();
    }
}
